<?php
/**
 *NewProduct Model
 *Creates a new products
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 *Class NewProduct_Model
 *
 *@package model
 */

class NewProduct_Model extends Model{
        /**
     * Holds the database reference
     * @var object
     */
    protected $oDBInstance;

    /**
     * Model constructor
     * Establishes connection to database
     */
    public function __construct(){
        $this->oDBInstance = Mysql_dbconnection::getInstance();
    }
/**
 * Gets the category / page title in which the product should be created
 * @param integer category ID
 * @param integer language ID
 */
    public function getPageTitle($iCategoryID, $iLang){
        $this->oDBInstance->query("SELECT name
			FROM `bs_page` INNER JOIN bs_page_language ON id = pid
			WHERE type = 'category' AND id = $iCategoryID AND lid = $iLang;");
        
        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0]['name'])){
        $sPage = $aResult[0]['name'];
        }else{
            $sPage = "Keiner Kategorie - Fehler";
        }

        return "Neues Produkt in Kategorie $sPage anlegen";
    }

/**
 * Loads the product details in case of editing
 * @param integer product id in this case
 * @param integer language
 */
    public function getProductDetail($iPid, $iLang){
        $this->oDBInstance->query("SELECT *
			FROM `bs_product` AS p
			INNER JOIN `bs_product_language` AS l ON p.id = l.pid
			INNER JOIN bs_image AS i ON p.id = i.product
			WHERE p.id = '$iPid' AND l.lid = '$iLang'
			LIMIT 0 , 30");
        return $this->oDBInstance->fetch('array');

    }
/**
 * Gets the product ID 
 */
    public function getProductID(){
        $this->oDBInstance->query("SELECT MAX(id) as id FROM `bs_product`");
        $aID = $this->oDBInstance->fetch('array');
        $iID = $aID[0]['id'];
        return $iID;
    }

/**
 * Adds a new product in the database
 * @param array post array from the html form 
 * @param string category
 * @param integer language
 */
    public function addProduct($aPost,$iCa, $iLang){
        $sGermanShortDesc = "'Derzeit gibt es keine deutsche Beschreibung zu diesem Produkt.'";
        $sEnglishShortDesc = "'Currently there is no english description for this product.'";
        $sTurkishShortDesc = "'Derzeit gibt es keine t&uuml;rkische Beschreibung zu diesem Produkt.'";

        $sGermanLongDesc = "'Derzeit gibt es keine deutsche Beschreibung zu diesem Produkt.'";
        $sEnglishLongDesc = "'Currently there is no english description for this product.'";
        $sTurkishLongDesc = "'Derzeit gibt es keine t&uuml;rkische Beschreibung zu diesem Produkt.'";

       
        if(isset($aPost['name'],$aPost['short_description'],
        $aPost['long_description'])&& $aPost['name']!= ""){
            $this->oDBInstance = Mysql_dbconnection::getInstance();
            $sName = $this->oDBInstance->getSQLValueString($aPost['name'], 'string');
            $sShortDesc = $this->oDBInstance->getSQLValueString($aPost['short_description'], 'string');
            $sLongDesc = $this->oDBInstance->getSQLValueString($aPost['long_description'], 'string');
            $iLanguage = $this->oDBInstance->getSQLValueString($iLang, 'int');

            $iCa = $this->oDBInstance->getSQLValueString($iCa, 'int');
            
            //If short and long descritpton are not null
            if($sShortDesc != 'NULL' && $sLongDesc != 'NULL'){
                if($iLanguage == 3){
                    $sTurkishShortDesc = $sShortDesc;
                    $sTurkishLongDesc = $sLongDesc;
                }elseif($iLanguage == 2){
                    $sEnglishShortDesc = $sShortDesc;
                    $sEnglishLongDesc = $sLongDesc;
                }else{
                    $sGermanShortDesc = $sShortDesc;
                    $sGermanLongDesc = $sLongDesc;
                }
            }

            //Performs insert into bs_product and bs_product_language
            $this->oDBInstance->query("INSERT INTO `bs_product` (`id`,`name`, `mainimage`, `visible`, `deleted`, `category`) VALUES
                    (NULL,$sName, NULL, 1, 0, '$iCa');");
            
            $iPosition = $this->getMaxPosition($iCa);

            $this->oDBInstance->query("INSERT INTO `bs_product` (`id`,`name`, `mainimage`, `visible`, `deleted`, `category`, `position`) VALUES
                    (NULL,$sName, NULL, 1, 0, '$iCa', '$iPosition');");

            $iPID = self::getProductID();
            //Creates the product in all 3 languages 1 with the data from html form and the others with a standard text
            $this->oDBInstance->query("INSERT INTO `bs_product_language` (`pid`, `lid`, `short_description`, `long_description`) VALUES
                    ('$iPID','1', $sGermanShortDesc, $sGermanLongDesc);");
            $this->oDBInstance->query("INSERT INTO `bs_product_language` (`pid`, `lid`, `short_description`, `long_description`) VALUES
                    ('$iPID','2', $sEnglishShortDesc, $sEnglishLongDesc);");
            $this->oDBInstance->query("INSERT INTO `bs_product_language` (`pid`, `lid`, `short_description`, `long_description`) VALUES
                    ('$iPID','3', $sTurkishShortDesc, $sTurkishLongDesc);");
            return TRUE;
        }else
        echo "";

    }
    
    protected function getMaxPosition($iCa){
        
        $this->oDBInstance->query("SELECT MAX(position) as position FROM `bs_product` WHERE category = $iCa");

        $aResult = $this->oDBInstance->fetch('array');
        $iPosition = $aResult[0]['position'] +1;
        return $iPosition;
    }


}
?>